<?php
namespace addons\wanlshop\library\WanlPay;

class WanlPayDo
{
    // 红积分抵扣消费券处理
    public static function doRed($self_user,$all_discount,$order_id,$order_no,$shop_id){
        $user_id=$self_user['id'];
        // 下单本人获得商家让利同等数额的红积分,上三级,每级分别获得让利部分6个点的消费券,但是要拿获得消费券的5分之1的红积分换
        if($all_discount){
            // 出让计算
            $inc_red_points= round($all_discount,2);//本人获得的红积分
            $consum_price= round($all_discount*0.06,2);//推荐人获得的消费券
            $dec_red_points= round($all_discount*0.06/5,2);//推荐人需抵扣的红积分
    
            // 给自己增加红积分
            model('app\common\model\User')->where('id',$user_id)->inc('red_points',$inc_red_points)->update();

            // 写入红积分发放日志---本人加
            $SelfRedPointsLog['user_id']=$user_id;
            $SelfRedPointsLog['points']=$inc_red_points;
            $SelfRedPointsLog['balance']= $self_user['red_points']+$inc_red_points;
            $SelfRedPointsLog['order_id']=$order_id;
            $SelfRedPointsLog['order_no']=$order_no;
            $SelfRedPointsLog['order_user_id']=$user_id;
            $SelfRedPointsLog['order_shop_id']=$shop_id;
            $SelfRedPointsLog['get_type']=1;//获得或使用方式：1自己下单加2粉丝下单减
            model('app\common\model\RedPointsLog')->save($SelfRedPointsLog);
            // 查询人员
           
            // $self_user=model('app\common\model\User')->field('id,pid,red_points')->where('id',$user_id)->find();
            // 给推荐人发放消费券，并抵扣红积分
            $do_user=[];
            $user_arr=[];
            if($self_user['pid']){//一级
                $one_user=model('app\common\model\User')->field('id,pid,red_points,coupon_num')->where('id',$self_user['pid'])->find();
                $user_arr[0]=$one_user;
                if($one_user['red_points']>=$dec_red_points){
                    array_push($do_user,$one_user['id']);
                }
                if($one_user['pid']){//二级
                    $two_user=model('app\common\model\User')->field('id,pid,red_points,coupon_num')->where('id',$one_user['pid'])->find();      
                    $user_arr[1]=$two_user;
                    if($two_user['red_points']>=$dec_red_points){ 
                        array_push($do_user,$two_user['id']);
                    }
                    if($two_user['pid']){//三级
                        $three_user=model('app\common\model\User')->field('id,pid,red_points,coupon_num')->where('id',$two_user['pid'])->find();
                        $user_arr[2]=$three_user;
                        if($three_user['red_points']>=$dec_red_points){ 
                            array_push($do_user,$three_user['id']);
                        }
                    }
                }

                if($do_user){
                    $consumData=[];
                    $PidRedPointsLog=[];
                    foreach($do_user as $k => $do_user_id){
                        $consumData[$k]['user_id']=$do_user_id;
                        $consumData[$k]['money']=$consum_price;
                        $consumData[$k]['balance']=$user_arr[$k]['coupon_num']+$consum_price;
                        $consumData[$k]['order_id']=$order_id;
                        $consumData[$k]['order_no']=$order_no;
                        $consumData[$k]['order_user_id']=$user_id;
                        $consumData[$k]['order_shop_id']=$shop_id;
                        $consumData[$k]['get_type']=1;//获得或使用方式：1红积分兑换2绿积分3消费使用
    
                        $PidRedPointsLog[$k]['user_id']=$do_user_id;
                        $PidRedPointsLog[$k]['points']='-'.$dec_red_points;
                        $PidRedPointsLog[$k]['balance']=$user_arr[$k]['red_points']-$dec_red_points;
                        $PidRedPointsLog[$k]['order_id']=$order_id;
                        $PidRedPointsLog[$k]['order_no']=$order_no;
                        $PidRedPointsLog[$k]['order_user_id']=$user_id;
                        $PidRedPointsLog[$k]['order_shop_id']=$shop_id;
                        $PidRedPointsLog[$k]['get_type']=2;//获得或使用方式：1自己下单加2粉丝下单减
                    }
    
                    // 写入消费券日志--给推荐人发放消费券
                    model('app\common\model\ConsumCard')->saveAll($consumData);
    
                    // 给推荐人减红积分，并加消费券金额
                    model('app\common\model\User')->where('id','in',$do_user)->dec('red_points',$dec_red_points)->inc('coupon_num',$consum_price)->update();
    
                    // 写入红积分发放日志--推荐人减
                    model('app\common\model\RedPointsLog')->isUpdate(false)->saveAll($PidRedPointsLog);
                }
            }
        }
    }

    // 橙积分处理
    public static function doOrange($self_user,$all_discount,$order_id,$order_no,$shop_id){
        $user_id=$self_user['id'];
        if($all_discount){
            // 出让计算
            $inc_orange_points= round($all_discount,2);//本人获得的橙积分
          
            // 给自己增加橙积分
            model('app\common\model\User')->where('id',$user_id)->inc('orange_points',$inc_orange_points)->update();

            // 写入橙积分发放日志---本人加
            $SelfOrangePointsLog['user_id']=$user_id;
            $SelfOrangePointsLog['points']=$inc_orange_points;
            $SelfOrangePointsLog['balance']= $self_user['orange_points']+$inc_orange_points;
            $SelfOrangePointsLog['order_id']=$order_id;
            $SelfOrangePointsLog['order_no']=$order_no;
            $SelfOrangePointsLog['order_user_id']=$user_id;
            $SelfOrangePointsLog['order_shop_id']=$shop_id;
            $SelfOrangePointsLog['get_type']=1;//获得或使用方式：1自己下单加2粉丝下单减
            model('app\common\model\OrangePointsLog')->save($SelfOrangePointsLog);
        }
    }

    // VIP相关处理
    public static function doVip($self_user){
        $user_id=$self_user['id'];
        // 累计超过350元，升级成为vip，可购买专属商品
        if($self_user['level']==1&&($self_user['total_consum_price']>=350)){
            model('app\common\model\User')->where('id',$user_id)->update(['level'=>2]);
        }
    }

    // 星推官相关处理
    public static function doStarReference($self_user,$all_discount,$order_id,$order_no,$star_reference_userid,$shop_id){
        // 邀请的商家让利部分的6个点佣金,邀请超过10个多三个点
        if($star_reference_userid){//有星推官
            $star_user=model('app\common\model\User')->field('id,commission,level')->where('id',$star_reference_userid)->find();

            if(isset($star_user['level']) && $star_user['level']==3){
                $commission= round($all_discount*0.06,2);//星推官获得的佣金

                // 给星推官增加佣金
                model('app\common\model\User')->where('id',$star_reference_userid)->inc('commission',$commission)->update();
                
                // 写入佣金日志表
                $CommissionData['user_id']=$star_reference_userid;
                $CommissionData['money']=$commission;
                $CommissionData['balance']=$star_user['commission']+$commission;
                $CommissionData['get_type']=1;//获得或使用方式：1星推官佣金2区县服务商3提现
                $CommissionData['order_id']=$order_id;
                $CommissionData['order_no']=$order_no;
                $CommissionData['order_user_id']=$self_user['id'];
                $CommissionData['order_shop_id']=$shop_id;
                model('app\common\model\ConsumCommission')->insert($CommissionData);
            }
        }
    }

    // 区县服务商相关处理
    public static function doArea($self_user,$all_discount,$order_id,$order_no,$area_code,$shop_id){
        if($area_code){
            // 所在区县所有商家让利部分的8个点佣金
            $userWhere['level']=4;
            $userWhere['area_code']=$area_code;
            $area_userid= model('app\common\model\User')->field('id,commission')->where($userWhere)->find();
            if($area_userid){//有区县服务商
                $commission= round($all_discount*0.08,2);//星推官获得的佣金

                // 给区县服务商增加佣金
                model('app\common\model\User')->where('id',$area_userid['id'])->inc('commission',$commission)->update();
                
                // 写入佣金日志表
                $CommissionData['user_id']=$area_userid['id'];
                $CommissionData['money']=$commission;
                $CommissionData['balance']=$area_userid['commission']+$commission;
                $CommissionData['get_type']=2;//获得或使用方式：1星推官佣金2区县服务商3提现
                $CommissionData['order_id']=$order_id;
                $CommissionData['order_no']=$order_no;
                $CommissionData['order_user_id']=$self_user['id'];
                $CommissionData['order_shop_id']=$shop_id;
                model('app\common\model\ConsumCommission')->insert($CommissionData);
            }
        }
    }

    // 全国运营中心相关处理
    public static function doNational($self_user,$all_discount,$order_id,$order_no,$shop_id){
        $national_userid_level=$self_user['national_userid_level'];//全国运营中心用户id/所在层级
        // 下面9级消费商家让利的15%消费券
        if($national_userid_level){
            $national_level=explode('/',$national_userid_level)[1];//所在层级
            if($national_level<=9 && $national_level>0){//层级小于9
               $national_user_id=explode('/',$national_userid_level)[0];//全国运营中心用户id

               $national_user=model('app\common\model\User')->field('id,coupon_num,level')->where('id',$national_user_id)->find();
               if(isset($national_user['level']) && $national_user['level']==5){
                    $card= round($all_discount*0.15,2);//全国运营中心获得的消费券

                    // 给全国运营中心发放消费券
                    model('app\common\model\User')->where('id',$national_user_id)->inc('coupon_num',$card)->update();

                    // 写入消费券日志表
                    $consumData['user_id']=$national_user_id;
                    $consumData['money']=$card;
                    $consumData['balance']=$national_user['coupon_num']+$card;
                    $consumData['order_id']=$order_id;
                    $consumData['order_no']=$order_no;
                    $consumData['order_user_id']=$self_user['id'];
                    $consumData['order_shop_id']=$shop_id;
                    $consumData['get_type']=4;//获得或使用方式：1红积分兑换2绿积分3消费使用4全国运营中心
                    model('app\common\model\ConsumCard')->insert($consumData);
               }
            }
        }
    }


    /**
     * 处理绿积分
     */
    public static function jifenGreen($goods = [], $userid = '', $orderid = '')
    {
        try {
            error_reporting(0);
            $total = 0;
            $shopusers = [];
            $shoparr = [];
            foreach($goods as $indes => $goodid){
                $goodsarr=model('app\api\model\wanlshop\Goods')->where('id', $goodid)->find();
                $score = $goodsarr['price']*($goodsarr['jifen_green']?:20)/100;
                $total += $score;

                //商家
                $shopuid = model('app\api\model\wanlshop\Shop')->where('id', $goodsarr['shop_id'])->value('user_id');
                if(in_array($shopuid, $shopusers)){
                    foreach ($shoparr as $key => $res) {
                        if ($res['uid'] == $shopuid) {
                            $shoparr[$key]['val'] = round($res['val'] + $score ,2);
                        }
                    }
                }else{
                    $shopusers[] = $shopuid;
                    $shoparr[$indes]['uid'] = $shopuid;
                    $shoparr[$indes]['val'] = round($score,2);
                }
            }
            $value = round($total,2);

            // 积分
            model('app\common\model\Greenjifenlog')->insert([
                'user_id' => $userid,
                'value' => $value,
                'order_id' => $orderid,
                'createtime' => time(),
                'updatetime' => time()
            ]);
            model('app\common\model\User')->where('id',$userid)->inc('jifenuser_green',$value)->update();

            // 商家积分
            foreach ($shoparr as $shop){
                model('app\common\model\Greenjifenlog')->save([
                    'user_id' => $shop['uid'],
                    'value' => $shop['val'],
                    'role' => 1,
                    'order_id' => $orderid
                ]);
                model('app\common\model\User')->where('id',$shop['uid'])->inc('jifenuser_green',$shop['val'])->update();
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

}